package 中等;

import java.util.HashMap;
import java.util.Map;

public class 无重复字符的最长子串3 {
    public static void main(String[] args) {
        String s = "pwwkew";
        System.out.println(lengthOfLongestSubstring(s));

    }

    public static int lengthOfLongestSubstring(String s){

        int length = s.length();
        if (length==0){
            return 0;
        }
        int left = 0;
        int max = 1;
        Map<Character,Integer> map = new HashMap<>();
        for (int i = 0; i < length; i++) {
            char c = s.charAt(i);
            if (map.containsKey(c)){
                left = Math.max(left, map.get(c)+1);
            }
            map.put(c,i );
            max = Math.max(max,i-left+1 );
        }
        return max;

    }
}
